package com.edu.koudingmusic.activities;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.edu.koudingmusic.BaseActivity;
import com.edu.koudingmusic.BaseApplication;
import com.edu.koudingmusic.R;
import com.edu.koudingmusic.adapters.MyMusicListAdapter;
import com.edu.koudingmusic.entities.Mp3Info;
import com.edu.koudingmusic.services.PlayService;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

public class MyLikeMusicListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "MyLikeMusicListActivity";
    private ListView mListViewLike;
    private BaseApplication mApp;
    // 两边的mMp3Infos 需要同步
    private ArrayList<Mp3Info> mMp3Infos;
    private MyMusicListAdapter mAdapter;

    // 表示当前播放列表是否为收藏列表
    private boolean isChange =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApp = ((BaseApplication) getApplication());
        setContentView(R.layout.activity_my_like_music_list);
        mListViewLike = (ListView) findViewById(R.id.listView_favorite);
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        bindPlayService();
    }


    @Override
    protected void onPause() {
        super.onPause();
        unBindPlayService();
    }

    private void initData() {
        try {
            List<Mp3Info> list = mApp.sDbUtils.findAll(Selector.from(Mp3Info.class).where("isLike", "=", "1"));
            if (list==null ||list.size()==0){
                return;
            }
            mMp3Infos = (ArrayList<Mp3Info>) list;
            mAdapter = new MyMusicListAdapter(this, mMp3Infos);
            mListViewLike.setAdapter(mAdapter);
            mListViewLike.setOnItemClickListener(this);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void publish(int progress) {

    }

    @Override
    public void change(int position) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (mPlayService.getChangeList()!= PlayService.MY_LIKE_MUSIC_LIST){
            mPlayService.setMp3Infos(mMp3Infos);
            mPlayService.setChangeList(PlayService.MY_LIKE_MUSIC_LIST);
        }
        mPlayService.play(position);

        // 保存播放时间
        savePlayRecord();

    }

    private void savePlayRecord() {

        // 获取当前正在播放的音乐对象
        Mp3Info mp3Info = mPlayService.getMp3Infos().get(mPlayService.getCurrentPosition());
        try {
            Mp3Info playRecordMp3Info = mApp.sDbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId", "=", mp3Info.getMp3InfoId()));
            if (playRecordMp3Info == null) {
                // 设置当前播放时间
                mp3Info.setPlayTime(System.currentTimeMillis());
                mApp.sDbUtils.save(mp3Info);
                Log.e(TAG, "onClick: save");
            } else {
                playRecordMp3Info.setPlayTime(System.currentTimeMillis());
                mApp.sDbUtils.update(playRecordMp3Info, "playTime");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
